package com.xindz.hgq.base;

import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kingja.loadsir.core.LoadSir;
import com.xindz.hgq.R;

/**
 * @name HGQBase
 * @class name：com.xindz.hgq.title
 * @describe
 * @anthor XINDZ
 * @time 2018/1/11 14:01
 * @change
 * @chang time
 * @class describe
 */
public abstract class BaseTitleActivity<V extends BaseView, P extends BasePresenter<V>> extends BaseActivity<V, P> {
    private View rootView;

    private FrameLayout flBaseContent;
    /**
     * Title主布局
     */
    private RelativeLayout titleLayout;
    /**
     * 右侧父布局
     */
    private LinearLayout titleRightLayout;
    /**
     * 左侧返回父布局
     */
    private LinearLayout titleBackLayout;
    /**
     * 自定义标题布局
     */
    private FrameLayout titleCenterDefinedLayout;
    /**
     * 左侧返回ICON
     */
    private ImageView ivTitleLeftBack;
    /**
     * 右侧ICON
     */
    private ImageView ivTitlerightImg;
    /**
     * 右侧文字
     */
    private TextView tvTitleRightText;
    /**
     * 中间标题
     */
    private TextView tvTitleCenter;
    /**
     * 中间父布局
     */
    private RelativeLayout titleCenterTextLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    /**
     * 重写了setContentView 方法
     * 如果Options中开启了OpenTitle
     * 则用AddView的方法添加Title
     * 否则使用super.setContentView(layoutResID);
     *
     * @param layoutResID
     */
    @Override
    public void setContentView(int layoutResID) {
        if (getOptions().isOpenTitle()) {
            rootView = View.inflate(this, R.layout.layout_base_activity, null);

            titleLayout = (RelativeLayout) rootView.findViewById(R.id.rl_base_title);
            titleRightLayout = (LinearLayout) rootView.findViewById(R.id.ll_title_right);
            titleBackLayout = (LinearLayout) rootView.findViewById(R.id.ll_title_left);
            titleCenterTextLayout = (RelativeLayout) rootView.findViewById(R.id.rl_title_center_text);

            titleCenterDefinedLayout = (FrameLayout) rootView.findViewById(R.id.fl_title_center);
            flBaseContent = (FrameLayout) rootView.findViewById(R.id.fl_base_content);

            ivTitleLeftBack = (ImageView) rootView.findViewById(R.id.iv_title_left_back);
            ivTitlerightImg = (ImageView) rootView.findViewById(R.id.iv_title_right_img);
            tvTitleRightText = (TextView) rootView.findViewById(R.id.tv_title_right_text);
            tvTitleCenter = (TextView) rootView.findViewById(R.id.tv_title_center);


            View content = View.inflate(this, layoutResID, null);
            if (content != null) {
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
                flBaseContent.addView(content, layoutParams);
                //如果开启LaodSir
                if (getOptions().isOpenLoadSir()) {
                    loadService = LoadSir.getDefault().register(content, getOptions().getLoadSirListener());
                }
            }

            setContentView(rootView);

        } else {
            super.setContentView(layoutResID);
        }
    }


    /**
     * 设置左侧返回键 点击返回
     */
    public void showLeftBack() {
        showLeftButton(R.drawable.ic_back_toleft, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 设置TOOLBAR 左侧ICON 和点击事件
     *
     * @param iconId
     * @param listener
     */
    public void showLeftButton(@DrawableRes int iconId, View.OnClickListener listener) {
        if (getOptions().isOpenTitle() && ivTitleLeftBack != null && titleBackLayout != null) {
            titleBackLayout.setVisibility(View.VISIBLE);
            //设置左侧按钮ICON
            ivTitleLeftBack.setImageResource(iconId);
            //设置左侧按钮点击事件
            titleBackLayout.setOnClickListener(listener);
        }
    }

    /**
     * 设置标题右边文字
     *
     * @param titleRightText
     * @return 返回右边布局用于设置点击事件
     */
    public LinearLayout setTitleRightText(String titleRightText) {
        titleRightLayout.setVisibility(View.VISIBLE);
        tvTitleRightText.setVisibility(View.VISIBLE);
        ivTitlerightImg.setVisibility(View.GONE);
        tvTitleRightText.setText(titleRightText);
        return titleRightLayout;
    }

    /**
     * 设置标题右边文字
     *
     * @param titleRightImage
     * @return 返回右边布局用于设置点击事件
     */
    public LinearLayout setTitleRightImage(@DrawableRes int titleRightImage) {
        titleRightLayout.setVisibility(View.VISIBLE);
        tvTitleRightText.setVisibility(View.GONE);
        ivTitlerightImg.setVisibility(View.VISIBLE);
        ivTitlerightImg.setBackgroundResource(titleRightImage);
        return titleRightLayout;
    }

    /**
     * 标题中间填充布局
     *
     * @param resource 标题中间的布局
     * @return
     */
    public View setTitleDefined(@LayoutRes int resource) {
        View titleView = View.inflate(this, resource, null);
        titleCenterDefinedLayout.addView(titleView);
        titleCenterDefinedLayout.setVisibility(View.VISIBLE);
        titleCenterTextLayout.setVisibility(View.GONE);
        return titleView;
    }

    /**
     * 设置文字标题
     *
     * @param titleText
     */
    public void setTitleText(String titleText) {
        titleCenterDefinedLayout.setVisibility(View.GONE);
        titleCenterTextLayout.setVisibility(View.VISIBLE);
        tvTitleCenter.setText(titleText);
    }

}
